package com.domain.service.impl;

import com.domain.common.Result;
import com.domain.dao.*;
import com.domain.pojo.*;
import com.domain.service.UsersHelpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsersHelpServiceImpl implements UsersHelpService {
    @Autowired
    private UsersHelpMapper usersHelpMapper;


    @Autowired
    private CommentMapper commentMapper;

    /**
     * 获取我的帮转列表
     *
     * @param userId   用户id
     * @param pageNum  页码
     * @param pageSize
     * @return
     */
    @Override

    public Result getUsersPublishHelpList(String userId, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(20));
        List<Map<String, Object>> helps = usersHelpMapper.selectByUsersId(userId);
        //评论列表
        List<Comment> size = new ArrayList<>();
        for (Map<String, Object> map : helps) {
            size = commentMapper.selectBypublishId(map.get("pid").toString());
        }
        if (helps.size() == 0) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("uid", "");
            maps.put("uname", "");
            maps.put("unickname", "");
            maps.put("helpTime", "");
            maps.put("headImage", "");
            maps.put("pid", "");
            maps.put("ptitle", "");
            maps.put("ptype", "");
            maps.put("pItIsPaid", "");
            maps.put("redPackgeNum", "");
            maps.put("forwardCount", "");
            helps.add(maps);
        }
        PageInfo list = new PageInfo(helps);
        Map<String, Object> maps = new HashMap<>();
        maps.put("helpList", list.getList());
        maps.put("commentSize", size.size());
        return Result.success("操作成功", maps);
    }

    /**
     * 删除我的帮转
     *
     * @param helpId 帮转id
     * @return
     */
    @Override
    public Result deleteHelpPublish(String helpId) {
        usersHelpMapper.deleteByPrimaryKey(helpId);
        return Result.success();
    }
}
